﻿@inject IWikiService WikiService
@inject IStorageService StorageService
@inject IPopupService PopupService

<MDialog @bind-Value="_visible"
         Fullscreen
         Transition="dialog-bottom-transition">
    <ActivatorContent>
        <MListItem Link
                   @attributes="@context.Attrs">
            <MListItemIcon>
                <MIcon>mdi-file-chart-outline</MIcon>
            </MListItemIcon>
            <MListItemContent>
                <MListItemTitle>本地文件</MListItemTitle>
            </MListItemContent>
        </MListItem>
    </ActivatorContent>
    <ChildContent>
        <MCard>
            <MToolbar Dark
                      Color="primary">
                <MToolbarTitle>上传本地文件</MToolbarTitle>
                <MSpacer></MSpacer>
                <MToolbarItems>
                    <MButton Icon
                             Dark
                             OnClick="() => _visible = false">
                        <MIcon>mdi-close</MIcon>
                    </MButton>
                </MToolbarItems>
            </MToolbar>
            <MDivider></MDivider>
            <MStepper Value="_step">
                <MStepperHeader>
                    <MStepperStep Step="1" Complete="_step>1">
                        选择文件
                    </MStepperStep>
                    <MDivider></MDivider>
                    <MStepperStep Step="2" Complete="_step>2">
                        数据处理
                    </MStepperStep>
                    <MDivider></MDivider>
                    <MStepperStep Step="3">
                        上传数据
                    </MStepperStep>
                </MStepperHeader>

                <MStepperItems>
                    <MStepperContent Step="1">
                        <MCard Outlined Class="mb-12">
                            
                            <MSwitch @bind-Value="Directory"
                                     LeftText="文件"
                                     RightText="目录"
                                     Style="margin-left: 50%;"
                                     Color="black"
                                     TrackColor="#E5E6EB">
                            </MSwitch>

                            <DropUpload Accept="@string.Join(',', _types)" Multiple Directory="Directory" OnUploadChanged="UploadChanged">
                                <div class="flex flex-col items-center justify-center h-full upload-item">
                                    <MIcon Size="48">mdi-file-upload</MIcon>
                                    <div class="text-h6">拖拽文件到此处</div>
                                    <div class="text-caption">或点击上传</div>
                                </div>
                            </DropUpload>
                            <MCardActions Style="display: block; overflow: auto; max-height: 60vh;">
                                @foreach (var item in _files)
                                {
                                    <MChip Class="ma-2"
                                           Close
                                           Color="green"
                                           TextColor="white"
                                           OnCloseClick="() => RemoveFile(item.Key)">
                                        @item.Key.Name
                                        <span style="font-size: 10px; margin-left: 4px;">
                                            文件大小：@StringHelper.FormatBytes(item.Key.Size)
                                        </span>
                                    </MChip>
                                }
                            </MCardActions>
                            <MCardActions>
                                <MButton Color="primary" Block OnClick="SelectFileHandle">下一步</MButton>
                            </MCardActions>
                        </MCard>
                    </MStepperContent>
                    <MStepperContent Step="2">
                        <MCard Outlined Class="mb-12" Height="@("calc(100vh - 230px)")">
                            <div style="display: flex;">
                                <div style="margin:8px;width:15%;">
                                    <div style="margin-top:8px;">
                                        <p>训练模式</p>
                                        <MRadioGroup @bind-Value="_trainingPattern"
                                                     Row>
                                            <MRadio Label="直接拆分"
                                                    Value="TrainingPattern.Subsection">
                                            </MRadio>
                                            <MRadio Label="问答拆分"
                                                    Value="TrainingPattern.QA">
                                            </MRadio>
                                        </MRadioGroup>
                                    </div>
                                    <div style="margin-top:8px;">
                                        <p>处理方式</p>
                                        <MRadioGroup @bind-Value="_processMode"
                                                     Row>
                                            <MRadio Label="自动处理"
                                                    Value="ProcessMode.Auto">
                                            </MRadio>
                                            <MRadio Label="自定义处理"
                                                    Value="ProcessMode.Custom">
                                            </MRadio>
                                        </MRadioGroup>
                                    </div>
                                    @if (_trainingPattern == TrainingPattern.Subsection && _processMode == ProcessMode.Custom)
                                    {
                                        <div style="margin-top:15px;">

                                            <MTextField Outlined NumberProps="@(prop => { prop.Min = 200; prop.Max = 2048; prop.Step = 1; })" Type="number" TValue="int" @bind-Value="subsection" Label="理想分段长度"></MTextField>

                                        </div>
                                    }
                                    <MButton Block Color="primary" OnClick="Preview">预览</MButton>
                                </div>
                                <div style="flex: 1;margin:15px;height: 70vh;overflow: auto;">
                                    @foreach (var item in _files)
                                    {
                                        @foreach (var input in item.Value)
                                        {
                                            <MCard Style="margin:8px;"
                                                   Outlined>
                                                <MCardTitle Style="font-size:16px;font-weight:bold;">
                                                    @input.Name @("(#" + (item.Value.IndexOf(input) + 1) + ")")
                                                </MCardTitle>
                                                <div style="white-space: pre-line;margin:8px;">
                                                    @input.Content
                                                </div>
                                            </MCard>
                                        }
                                    }
                                </div>
                            </div>
                            <MCardActions>
                                <MSpacer></MSpacer>
                                <MButton Color="primary" OnClick="() => _step = 1">上一步</MButton>
                                <MButton Color="primary" OnClick="() => _step = 3">下一步</MButton>
                            </MCardActions>
                        </MCard>
                    </MStepperContent>
                    <MStepperContent Step="3">
                        <MCard Class="mb-12" >
                            <div style="display: flex;">
                                <MDataTable Headers="_headers"
                                            Items="BrowserFiles"
                                            Style="width:100%;"
                                            Class="elevation-1">
                                    <ItemColContent>
                                        @if (context.Header.Value == nameof(UploadSubsectionInput.Name))
                                        {
                                            <span>@context.Value</span>
                                        }
                                        else if (context.Header.Value == nameof(UploadSubsectionInput.Count))
                                        {
                                            <span>@context.Value</span>
                                        }
                                        else if (context.Header.Value == nameof(UploadSubsectionInput.FileProgress))
                                        {
                                            <MProgressLinear Value="@((int)context.Value)" Height="10" Context="progress">
                                                <strong>@($"{progress}%")</strong>
                                            </MProgressLinear>
                                        }
                                        else if (context.Header.Value == nameof(UploadSubsectionInput.DataProgress))
                                        {
                                            <MProgressLinear Value="@((int)context.Value)" Height="10" Context="progress">
                                                <strong>@($"{progress}%")</strong>
                                            </MProgressLinear>
                                        }
                                    </ItemColContent>
                                </MDataTable>
                            </div>
                            <MCardActions>
                                <MSpacer></MSpacer>
                                <MButton Color="primary" OnClick="()=>_step=2">上一步</MButton>
                                <MButton Color="primary" OnClick="Upload">共@(BrowserFiles.Count)个文件|开始上传</MButton>
                            </MCardActions>
                        </MCard>
                    </MStepperContent>
                </MStepperItems>
            </MStepper>
        </MCard>
    </ChildContent>
</MDialog>